package com.xxgc.eow.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xxgc.eow.entity.Topiclibrary;
import com.xxgc.eow.service.ITopiclibraryService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import com.xxgc.eow.result.Result;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.xxgc.eow.service.IRecommendlibraryService;
import com.xxgc.eow.entity.Recommendlibrary;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Abel
 * @since 2023-05-25
 */
@Tag(name = "每日推荐接口")
@RestController
@RequestMapping("/recommendlibrary")
public class RecommendlibraryController {

    @Resource
    private IRecommendlibraryService recommendlibraryService;

    @Resource
    private ITopiclibraryService topiclibraryService;

    /**
     * 查询所有接口
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "查询所有接口", description = "查询所有数据")
    @SaCheckLogin
    @GetMapping
    public Result<List<Recommendlibrary>> findAll() {
        return Result.ok(recommendlibraryService.list());
    }

    /**
     * 根据id查询数据接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "根据id查询数据接口", description = "通过组件id查询")
    @Parameters({
            @Parameter(name = "id",description = "组件id",required = true,in=ParameterIn.QUERY)
    })
    @SaCheckLogin
    @GetMapping("/{id}")
    public Result<Recommendlibrary> findOne(@PathVariable Integer id) {
        return Result.ok(recommendlibraryService.getById(id));
    }

    /**
     * 分页查询接口
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "分页查询接口", description = "分页查询")
    @Parameters({
            @Parameter(name = "pageNum",description = "页码",example="1",required = true,in=ParameterIn.QUERY),
            @Parameter(name = "pageSize",description = "每页多少条",example="10",required = true,in=ParameterIn.QUERY)
    })
    @SaCheckLogin
    @GetMapping("/page")
    public Result<Page<Recommendlibrary>> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        QueryWrapper<Recommendlibrary> queryWrapper = new QueryWrapper<>();
        return Result.ok(recommendlibraryService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    /**
     * 新增和更新接口
     * @param
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "新增和更新接口", description = "传入id为更新,不传为新增,时间和有效字段均不填")
    @SaCheckLogin
    @PostMapping
    public Result save(@RequestBody Recommendlibrary recommendlibrary) {
        recommendlibraryService.saveOrUpdate(recommendlibrary);
        return Result.ok();
    }

    /**
     * 删除接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "删除接口", description = "通过id删除")
    @Parameters({
            @Parameter(name = "id",description = "组件id",required = true,in=ParameterIn.QUERY)
    })
    @SaCheckLogin
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        recommendlibraryService.removeById(id);
        return Result.ok();
    }

    /**
     * 批量删除接口
     * @param ids
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "批量删除接口", description = "批量删除接口")
    @SaCheckLogin
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        recommendlibraryService.removeByIds(ids);
        return Result.ok();
    }

    /**
     * 生成推荐接口
     * @param
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "生成推荐", description = "生成每日推荐的题，如果本日已经生成则不生成")
    @SaIgnore
    @PostMapping("/generate")
    public Result generate() {
        if(recommendlibraryService.getNewRecommendlibrary().getCreateTime().plusDays(1).isBefore(LocalDateTime.now())){
            List<Topiclibrary> topiclibraries = topiclibraryService.list();
            Recommendlibrary recommendlibrary = new Recommendlibrary();
            recommendlibrary.setTopicLibraryId(topiclibraries.get(new Random().nextInt(topiclibraries.size())).getTopicLibraryId());
            recommendlibraryService.save(recommendlibrary);
            return Result.ok();
        }else{
            return Result.ok("本日推荐已经存在，无法生成");
        }
    }

    /**
     * 获取最新推荐
     * @param
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "获取推荐", description = "获取最新的每日推荐")
    @SaIgnore
    @GetMapping("/getNew")
    public Result getNew() {
        Recommendlibrary recommendlibrary = recommendlibraryService.getNewRecommendlibrary();
        if(recommendlibrary.getCreateTime().plusDays(1).isBefore(LocalDateTime.now())){
            //距离上次每日推荐过了一天
            List<Topiclibrary> topiclibraries = topiclibraryService.list();
            Recommendlibrary temp = new Recommendlibrary();
            temp.setTopicLibraryId(topiclibraries.get(new Random().nextInt(topiclibraries.size())).getTopicLibraryId());
            recommendlibraryService.save(temp);
            return Result.ok(recommendlibraryService.getNewRecommendlibrary());
        }else{
            //返回当前每日推荐
            return Result.ok(recommendlibrary);
        }
    }
}